# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from google.appengine.api import urlfetch
import webapp2

import cache
import cloudstorage
import config
import content_type
from urlparse import urlparse

class PreCacherFromUrl(webapp2.RequestHandler):
  #
  # For local testing, put the revision file and the zip archive
  # into the local GS server from your browser (e.g. for revision 155046):
  #
  # http://localhost:8080/precache_url?url=http://commondatastorage.googleapis.com/chrome-devtools-frontend/revs/@155046
  #
  # Then pre-cache the archive (you need to look up MD5 from the meta-file from the 'revs' metafile):
  #
  # http://localhost:8080/precache_url?url=http://commondatastorage.googleapis.com/chrome-devtools-frontend/zips/e4eb112917d276dac7b43affa30422143f37fd26.zip
  #
  # Then this works: http://localhost:8080/serve_rev/@155046/devtools.html
  #
  def get(self):
    if config.IS_DEV_APP_SERVER:
      url = self.request.GET['url']
      self.response.headers['Content-Type'] = 'text/plain'
      self.response.write(precache_url(url))
    else:
      self.abort(403)

def precache_url(url):
  result = urlfetch.fetch(url)
  file_name = urlparse(url).path
  if result.status_code == 200:
    gcs_file = cloudstorage.open(file_name,
                                 'w',
                                 content_type=content_type.from_path(file_name))
    gcs_file.write(result.content)
    gcs_file.close()
    return 'Saved %s as %s' % (url, file_name)
  else:
    return 'Error: %d for %s' % (result.status_code, url)
